﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;
using System.Windows.Forms;
using LogLib;

namespace KXmlUIControl
{
    public class XmlConfigApplication
    {    
        /// <summary>
        /// 最新错误信息
        /// </summary>
        public string Error { get; private set; }
        public WindowType FormType = WindowType.MainForm;
        private XDocument _xmlDoc = null;
        private XElement _root = null;
        private string _file = "";

        public XmlFormObject MainForm { private set; get; } = new XmlFormObject();

        public XmlFormObject ChildForm { private set; get; } = new XmlFormObject();

        public List<XmlModuleObject> ListModules = new List<XmlModuleObject>();

        public List<XmlProgramObject> ListPrograms = new List<XmlProgramObject>();

        public XmlConfigForm MainFormConfig {private set; get; } = null;
        public XmlConfigForm ChildFormConfig { private set; get; } = null;

        /// <summary>
        /// 默认应用程序的名称
        /// </summary>
        public string DefaultAppName
        {
            private set;
            get;
        }

        /// <summary>
        /// 启动系统应用程序的名称
        /// </summary>
        public string StartupAppName
        {
            private set;
            get;
        }

        /// <summary>
        /// 主页应用程序的名称
        /// </summary>
        public string HomeAppName
        {
            private set;
            get;
        }
        public XmlConfigApplication(string file)
        {
            _file = file; 
        }

        public bool OnReading()
        {
            if (!OpenXmlDOC())
            {
                return false;
            }
            LoadMainFormConfig();
            LoadChildFormConfig();            
            ListModules = KXmlParser.GetXmlObject<XmlModuleObject>(_root, "Modules", "Module");
            ListPrograms = KXmlParser.GetXmlObject<XmlProgramObject>(_root, "Programs", "Program");
            StartupAppName = GetSectionConfigName("StartupApp");
            HomeAppName = GetSectionConfigName("HomeApp");
            DefaultAppName = GetSectionConfigName("DefaultApp");
            return true;
        }

        private void LoadMainFormConfig()
        {
            XElement formRoot = _root.Element("MainWindow");
            MainFormConfig = new XmlConfigForm(formRoot, true);
            MainFormConfig.LoadConfig();
            MainForm = MainFormConfig.FormConfig;
        }

        private void LoadChildFormConfig()
        {
            XElement formRoot = _root.Element("SubWindow");
            ChildFormConfig = new XmlConfigForm(formRoot, false);
            ChildFormConfig.LoadConfig();
            ChildForm = ChildFormConfig.FormConfig;
        }

        public bool SetDefaultAppName(string name)
        {
            if (!OpenXmlDOC())
            {
                return false;
            }
            XElement node = _root.Element("DefaultApp");
            if (node != null)
            {
                node.SetAttributeValue("Name", name);
                _xmlDoc.Save(_file);
                return true;
            }
            Error = "找不到配置节点: DefaultApp";
            return false;
        }

        private string GetSectionConfigName(string section)
        {
            XElement node = _root.Element(section);
            if(node != null)
            {
                string Name = node.Attribute("Name").Value.Trim();
                Log.WriteAsync(string.Format("Section:{0}={1}", section, Name));
                return Name;
            }
            return "";
        }

        private bool OpenXmlDOC()
        {
            try
            {
                _xmlDoc = XDocument.Load(_file);
                _root = _xmlDoc.Element("Windows");
                string typeVal = _root.Attribute("Type").Value.Trim();
                FormType = (WindowType)Enum.Parse(typeof(WindowType), typeVal);
                return true;
            }
            catch (Exception ex)
            {
                Error = string.Format("配置文件{0}加载错误:{1}", _file, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }           
        }     
    }
}
